﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MOClient
{
    public partial class MainForm
    {
        string strTempJpegFile = @"c:/Inetpub/scripts/tempImg.jpg";
        string f_strTmpPath = @"c:/Inetpub/scripts/IMSAdminUsrImages/";
        long f_lCounter;
        string name = "MOclient";
        MapObjects.Rectangle previousExt;

        private void axWebLink1_Request(object sender, AxMoIMS._DWebLinkEvents_RequestEvent e)
        {
            try
            {
                MoIMS.Strings arguments = e.arguments as MoIMS.Strings;
                MoIMS.Strings values = e.values as MoIMS.Strings;
                string click, cmd, tmp, username, imgGuid;
                long clickX = 0, clickY = 0;
                bool layer1Visible, layer2Visible, layer3Visible, layer4Visible, layer5Visible, layer6Visible;
                bool layer7Visible, layer8Visible, layer9Visible, layer10Visible, layer11Visible, layer12Visible;
                MapObjects.Rectangle ext = new MapObjects.Rectangle();
              

                tmp = FindArgValue("left", arguments, values);

                previousExt = axMap1.Extent;
                if (tmp != String.Empty)
                {
                    ext.Left = int.Parse(tmp);
                    ext.Bottom = int.Parse(FindArgValue("bottom", arguments, values));
                    ext.Right = int.Parse(FindArgValue("right", arguments, values));
                    ext.Top = int.Parse(FindArgValue("top", arguments, values));
                }
                else ext = axMap1.FullExtent;

               

                click = FindArgValue("click.x", arguments, values);
                if (click != String.Empty)
                {
                    clickX = int.Parse(click);
                    clickY = int.Parse(FindArgValue("click.y", arguments, values));
                }

                

                username = FindArgValue("username", arguments, values);

                if (username == String.Empty)
                {
                    throw new Exception("username argument is  empty.");
                }

                imgGuid = FindArgValue("imgguid", arguments, values);

                if (imgGuid == String.Empty)
                {
                    //throw new Exception("imgGuid argument is  empty.");
                    Guid imgtempGuid = Guid.NewGuid();
                    imgGuid = imgtempGuid.ToString();
                }

                cmd = FindArgValue("cmd", arguments, values);

                if (cmd == String.Empty)
                {
                    throw new Exception("cmd argument is  empty.");
                }

                layer1Visible = (FindArgValue("layer1Visible", arguments, values) != String.Empty )?
                bool.Parse(FindArgValue("layer1Visible", arguments, values))
                    : true;
                layer2Visible = (FindArgValue("layer2Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer2Visible", arguments, values))
                    : true;
                layer3Visible = (FindArgValue("layer3Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer3Visible", arguments, values))
                    : true;
                layer4Visible = (FindArgValue("layer4Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer4Visible", arguments, values))
                    : true;
                layer5Visible = (FindArgValue("layer5Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer5Visible", arguments, values))
                    : true;
                layer6Visible = (FindArgValue("layer6Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer6Visible", arguments, values))
                    : true;
                layer7Visible = (FindArgValue("layer7Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer7Visible", arguments, values))
                    : true;
                layer8Visible = (FindArgValue("layer8Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer8Visible", arguments, values))
                    : true;
                layer9Visible = (FindArgValue("layer9Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer9Visible", arguments, values))
                    : true;
                layer10Visible = (FindArgValue("layer10Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer10Visible", arguments, values))
                    : true;
                layer11Visible = (FindArgValue("layer11Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer11Visible", arguments, values))
                    : true;
                layer12Visible = (FindArgValue("layer12Visible", arguments, values) != String.Empty) ?
                bool.Parse(FindArgValue("layer12Visible", arguments, values))
                    : true;

                if (cmd == "map") DoMapExtent(imgGuid, username, axMap1.FullExtent);
                else if (cmd == "mouseclick") DoMapExtent(imgGuid, username, axMap1.FullExtent);
                //else  if (strCMD="pan")       DoPan(name, username,ext,clickX,clickY);
                else if (cmd == "zoomout") DoZoomOut(imgGuid, username, previousExt, clickX, clickY);
                else if (cmd == "zoomin") DoZoomIn(imgGuid, username, previousExt, clickX, clickY);
                else if (cmd == "layerVisib") DoLayerVisibility(imgGuid, username, previousExt,
                      layer1Visible, layer2Visible, layer3Visible,
                      layer4Visible, layer5Visible, layer6Visible, 
                      layer7Visible, layer8Visible, layer9Visible, 
                      layer10Visible, layer11Visible, layer12Visible);
                else throw new Exception("Invalid cmd value.");

               // axWebLink1.WriteString("hello from client =) ");
               // axWebLink1.WriteString("Args count : " + (e.arguments as MoIMS.Strings).Count);
            }
            catch (Exception ex)
            {
                axWebLink1.WriteString("An error occured in WebLink Request. Detailed info: "
                    + ex.Message);
            }
        }       

        /////////////////////////////////////////////////////////////////////////////

        string CreateMap(string imgGuid, string username, MapObjects.Rectangle ext)
        {
            string strBMPFile, strJpegFile, strFile;

            strJpegFile = strTempJpegFile;
            File.Delete(strJpegFile);
            strFile = imgGuid;

            DirectoryInfo dir = new DirectoryInfo(strBMPFile = f_strTmpPath + username + @"/");
            if (!dir.Exists)
            {
                dir.Create();
            }

            strBMPFile = f_strTmpPath + username + @"/" + strFile + ".bmp";
            strTempJpegFile = f_strTmpPath + imgGuid + ".jpg";
            axMap1.Extent = ext;
            axMap1.ExportMap((short)MapObjects.ExportMapConstants.moExportBMP, strBMPFile, 1);
            axWebLink1.BMP2JPEG(strBMPFile, 90, true);
            File.Delete(strBMPFile);
            f_lCounter = f_lCounter + 1;
            return strFile + ".jpg";
        }

        ///////////////////////////////////////////////////////////////////////////
        private string FindArgValue(string str, MoIMS.Strings args, MoIMS.Strings values)
        {
            string Result = String.Empty;
            for (int i = 0; i <= (args).Count - 1; i++)
            {
                if ((args as MoIMS.Strings).Item(i) == str)
                {
                    return Result = (values).Item(i);
                }
            }
            return Result;
            //throw new Exception("Couldn't find value for parameter: " + str);
        }
    }
}

